from django.shortcuts import render, HttpResponse, redirect
from django.urls import reverse

from user.models import *


# Create your views here.
def get_users(request):
    """读取数据库内容并在页面展示"""
    users = UserModel.objects.all()
    return render(request, 'users.html', {"users": users})


def index(request):
    """页面跳转"""
    return render(request, 'user_index.html')


def user_list(request):
    """页面跳转"""
    users = UserModel.objects.all()
    return render(request, 'user_list.html', {"users": users})


def user_detail(request, uid):
    """用户详情"""
    user = UserModel.objects.get(pk=uid)
    return render(request, 'user_detail.html', {"user": user})


def user_ab_view(request, a, b):
    """显示2个用户"""
    return HttpResponse(f'a:{a} - b:{b}')


def my_redirect(request):
    """重定向"""
    return redirect('/user/userlist/')


def my_redirect1(request):
    """重定向"""
    # 位置参数传参，user是主路由中的namespace(如果没有namespace不写)，userdetail是子路由中的name
    # return redirect(reverse('user:userdetail', args=[1]))
    # 关键字传参，user是主路由中的namespace(如果没有namespace不写)，userdetail是子路由中的name
    return redirect(reverse('user:userdetail', kwargs={'uid': 2}))
